package org.lanqiao.tjut.control;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.lanqiao.tjut.bean.UsersEntity;
import org.lanqiao.tjut.service.IUserInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/user")
public class UserLoginControl {
	// 组合service层，依赖注入
	@Resource
	private IUserInfoService userService;
	
	@RequestMapping("/login")
	public ModelAndView doLogin2(HttpSession session,UsersEntity userE) {
		System.out.println("从表单获取的值为：name-" + userE.getUsername());
		//创建一个modelandview对象
		ModelAndView model = new ModelAndView();
		//设置逻辑视图名
		model.setViewName("logined");
		UsersEntity userE0 = userService.selectUsersInfo(2);
//		if(){
//			
//		}else{
//			
//		}
		//登录验证成功，将用户信息进行session缓存
		session.setAttribute("LoginedUserInfo", userE0);
		
		String strName = (userE0 == null ? "" : userE0.getUsername());
		System.out.println("数据库查询的用户信息为：" + strName);
		//向视图模型对象中添加 返回数据
		model.addObject("loggerName", strName);
		model.addObject("userE", userE);
		return model;
	}
	
	@RequestMapping("/checkValidate")
	
	public String doLogin9(@Validated @ModelAttribute("userE") UsersEntity userE,
			BindingResult br) {
		//检测验证是否通过
		if(br.hasErrors()){
			// 获取验证失败的信息
			List<ObjectError> lstErrors = br.getAllErrors();
			for (ObjectError objE : lstErrors) {
				// 打印错误信息
				System.out.println(objE.getDefaultMessage());
			}
			//TODO 扩展作业：如何打印出国际化的错误提示信息？
			// 阻止处理流程继续向下处理，返回到前台页面
			return "logined";
		}
		
		// 验证通过之后，本次请求的业务逻辑处理
		
		return "test";
	}
}
